package com.yc.thread;

public class TestSynchronized {
	
	public synchronized void m1(){
		for (int i = 0; i < 10; i++) {
			System.out.println("----------m1");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void m2(){
		for (int i = 0; i < 10; i++) {
			System.out.println("----------m2");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	final Object lockObj = new Object();
	public void m3() {
		synchronized (lockObj) {
			System.out.println("----------do something...");
		}
	}

	/**
	 * 锁对象：当前类的对象
	 * 功能：阻塞
	 */
	public synchronized void m4(){
		System.out.println("----------m4");
		try {
			this.wait();
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 锁对象：lockObj
	 * 功能：阻塞
	 */
	public  void m5(){
		synchronized(lockObj) {
			System.out.println("----------m5");
			try {
				lockObj.wait();
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public synchronized void m6() {
		this.notify();
	}

	//
	public static void main(String[] args) {
		TestSynchronized ts = new TestSynchronized();
//		Thread t1 = new Thread(() -> {
//			ts.m1();
//		});
//		Thread t2 = new Thread(() -> {
//			ts.m2();
//		});
//		t1.start();
//		t2.start();
		ts.m6();

	}

}
